﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Skynet.Framework.Common;
using DeliveryRoomStatusManage.Properties;
using BusinessFacade.His.Common;

namespace DeliveryRoomStatusManage
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //System.Runtime.Remoting.RemotingConfiguration.Configure("DeliveryRoomStatusBulletin.exe.config", false);
            Init();
        }

        public static void Init()
        {
            try
            {
                string localDBSource = Settings.Default.LocalDBSource;
                string localDBType = Settings.Default.LocalDBType;
                string hisDBSource = Settings.Default.HisDBSource;
                string hisDBType = Settings.Default.HisDBType;
                //string strPeriod_1 = Settings.Default
                //string SettingVal.Period_1.Add();


                Skynet.Data.PersistenceProperty.ConnectionString = localDBSource;
                Skynet.Data.PersistenceProperty.DatabaseType = (Skynet.Data.DatabaseType)Enum.Parse(typeof(Skynet.Data.DatabaseType), localDBType);

                Skynet.Data.PersistenceProperty.OtherConnectionStringList.Add(localDBSource);
                Skynet.Data.PersistenceProperty.OtherDatabaseType.Add((Skynet.Data.DatabaseType)Enum.Parse(typeof(Skynet.Data.DatabaseType), localDBType));

                Skynet.Data.PersistenceProperty.OtherConnectionStringList.Add(hisDBSource);
                Skynet.Data.PersistenceProperty.OtherDatabaseType.Add((Skynet.Data.DatabaseType)Enum.Parse(typeof(Skynet.Data.DatabaseType), hisDBType));

                //IDataAccess localDBAccess = Skynet.Data.DataAccessFactory.instance.CreateDataAccess(0);
                //IDataAccess hisDBAccess = Skynet.Data.DataAccessFactory.instance.CreateDataAccess(1);
                //try
                //{
                //    localDBAccess.Open();
                //    hisDBAccess.Open();
                //}
                //finally
                //{
                //    localDBAccess.Close();
                //    hisDBAccess.Close();
                //}

                //UpdateSystemConfig();


            }
            catch (Exception err)
            {
                MessageBox.Show("数据库配置文件不正确，详细:" + err.Message);
            }


        }

        private static void UpdateSystemConfig()
        {
            SystemConfigFacade sf = new SystemConfigFacade();
            DataSet sysConfigData = sf.FindAll();
            SystemConfigCollection sysConfigCollection = new SystemConfigCollection();
            Skynet.Framework.Common.SystemConfig sysConfig = null;
            foreach (DataRow row in sysConfigData.Tables[0].Rows)
            {
                sysConfig = new Skynet.Framework.Common.SystemConfig();
                //配置编号
                sysConfig.ConfigureNo = row["CONFIGURENO"].ToString().Trim();
                //设置类型
                sysConfig.SetType = row["SETTYPE"].ToString().Trim();
                //设置内容
                sysConfig.SetContent = row["SETCONTENT"].ToString().Trim();
                //设置值
                sysConfig.DefaultValue = row["DEFAULTVALUE"].ToString().Trim();
                //说明
                sysConfig.Detail = row["DETAIL"].ToString().Trim();
                //可选参数
                sysConfig.OptionalPara = row["OPTIONALPARA"].ToString().Trim();
                sysConfigCollection.Add(sysConfig);
            }
            SystemInfo.SystemConfigs = sysConfigCollection;
        }
    }
}
